define(['app'], function (app) {
    app.register.controller("UserCtrl", ["$scope", "$rootScope", "$http", "$modal", function ($scope, $rootScope, $http, $modal) {
        $rootScope.baseinfo.defaultMenuName = '系统管理';
        $rootScope.baseinfo.defaultSubMenuName = '用户管理';
        $scope.loadUserList=function(){
            $http.post('user/list.do', {})
                .success(function (data) {
                    $scope.userlist = data.data;
                });
        }
        $scope.addUserUI = function () {
            var scope = $rootScope.$new();
            scope.modal = {
                header: "增加用户",
                btnName: "保存",
                optType: "ADD",
                roles: ['普通用户', '管理员'],
                role: '普通用户',
                enables: ['启用', '禁用'],
                enable: '启用'
            }
            $scope.$modalInstance = $modal.open({
                templateUrl: 'views/user/edit.html',
                controller: "EditUserCtrl",
                scope: scope
            }).result.then(function () {
                    $scope.loadUserList();
            }, function () {
                // dismissed here.
                // e.g. backdrop click, escape key press
            });
        }
        $scope.getMyAppList=function(userid){
            var scope = $rootScope.$new();
            scope.userid = userid;
            $scope.$modalInstance = $modal.open({
                templateUrl: 'views/application/myapp.html',
                controller: "MyAppCtrl",
                scope: scope
            }).result.then(function () {
                    $scope.loadUserList();
                }, function () {
                    // dismissed here.
                    // e.g. backdrop click, escape key press
                });
        }

        $scope.loadUserList();

    }]).controller("EditUserCtrl", function ($scope, $modalInstance, $http) {

        $scope.ok = function () {
            var user = {
                "username": $scope.modal.username,
                "password": $scope.modal.password,
                "confirPassword": $scope.modal.confirPassword,
                "nickName": $scope.modal.nickName,
                "role": $scope.modal.role,
                "enable": $scope.modal.enable
            }
            $http({
                method: 'POST',
                url: 'user/add.do',
                data: user
            }).success(function (data) {
                $modalInstance.close();
            });

        };

        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }).controller("MyAppCtrl", function ($scope, $modalInstance, $http) {
        var data = {"userId":$scope.userid}
        $scope.loadMyAppList = function(){
            $http({
                method: 'POST',
                url: 'app/myapp.do',
                data: data
            }).success(function (data) {
                $scope.myapplist = data.data;
            });
        }
        $scope.editAppUser = function(uid,aid,type){
            var appuser ={
                uid:uid,
                aid:aid,
                type:type
            }
            $http({
                method: 'POST',
                url: 'app/editMyApp.do',
                data: appuser
            }).success(function (data) {
                $scope.loadMyAppList();
            });
        }

        $scope.cancel = function () {
            $modalInstance.close();
        };
        $scope.loadMyAppList();
    });;
    app.register.directive('pwCheck', [function () {
        return {
            require: 'ngModel',
            link: function (scope, elem, attrs, ctrl) {
                var firstPassword = '#' + attrs.pwCheck;
                elem.add(firstPassword).on('keyup', function () {
                    scope.$apply(function () {

                        var v = elem.val() === $(firstPassword).val();
                        ctrl.$setValidity('pwmatch', v);
                    });
                });
            }
        }
    }]);
});